<?php

namespace iBrand\Shop\Server\Controllers\V2;

use iBrand\Component\Goods\Models\Goods;
use iBrand\Component\Goods\Models\GoodsCategory;
use iBrand\Component\Goods\Models\Season;
use iBrand\Shop\Backend\Repository\O2oGoodsProductsRepository;
use iBrand\Shop\Backend\Service\CommonService;
use iBrand\Shop\Core\Models\Shop;
use iBrand\Shop\Server\Resources\StockQueryResource;
use iBrand\Common\Controllers\Controller;
use iBrand\Store\Backend\Repositories\CategoryRepository;

class StockQueryController extends Controller
{
    protected $o2oGoodsProductsRepository;
    protected $commonService;
    protected $categoryRepository;

    public function __construct(O2oGoodsProductsRepository $o2oGoodsProductsRepository,
                                CommonService $commonService,
                                CategoryRepository $categoryRepository)
    {
        $this->o2oGoodsProductsRepository = $o2oGoodsProductsRepository;
        $this->commonService = $commonService;
        $this->categoryRepository = $categoryRepository;
    }

    public function getGoodsSkuStoredList()
    {
        $clerk = auth('clerk')->user();
        $limit = request('limit') ? request('limit') : 15;
        $where['shop_id'] = request('shop_id') ? request('shop_id') : $clerk->shop_id;
        $ids = [];
        if (request('searchKey')) {
            $goods = Goods::where('name', 'like', '%' . request('searchKey') . '%')->orWhere('goods_no', 'like', '%' . request('searchKey') . '%')->get();
            if ($goods && $goods->count() > 0) {
                $ids = $goods->pluck('id')->toArray();
            }
        }


        //其他筛选条件
        $baseGoodsWhere['status'] = 1;
        if (request('season_id')) {
            $baseGoodsWhere['season_id'] = request('season_id');
        }
        if (request('years_id')) {
            $baseGoodsWhere['year'] = request('years_id');
        }
        if (request('brand_id')) {
            $baseGoodsWhere['brand_id'] = request('brand_id');
        }

        if (request('category_id')) {
            $category_goods = GoodsCategory::where('category_id', request('categoryId'))->get();
            if ($category_goods->count() > 0) {
                $c_goods_ids = $category_goods->pluck('goods_id')->toArray();
                $ids = array_unique(array_merge($ids, $c_goods_ids));
            }
        }


        $goods = $this->o2oGoodsProductsRepository->getGoodsProductsPaginate($where, $ids, ['baseGoods', 'baseProduct'], $limit, $baseGoodsWhere);

        $totalGoods = $this->o2oGoodsProductsRepository->getGoodsProductsPaginate($where, $ids, ['baseGoods', 'baseProduct'], 0, $baseGoodsWhere);
        $store_nums = 0;
        $purchaseAmount = 0;
        if ($totalGoods->count()) {
            $store_nums = $totalGoods->sum('store_nums');
            foreach ($totalGoods as $good) {
                $purchaseAmount += $good->store_nums * $good->baseGoods->factory_price;
            }
        }

        return $this->paginator($goods, StockQueryResource::class, ['store_nums' => $store_nums, 'purchaseAmount' => $purchaseAmount]);
    }

    /**
     * 库存查询条件
     * @return \Illuminate\Http\Response
     */
    public function getQueryFields()
    {
        $clerk = auth('clerk')->user();
        $shops = Shop::all()->toArray();
        $brandList = $this->commonService->getBrandList();
        $years = $this->commonService->getYearList();
        $categories = $this->categoryRepository->getLevelCategory(0, '&nbsp;&nbsp;');
        $seasons = Season::all()->toArray();

        $shops[count($shops)] = ['id' => 0, 'name' => '其他'];
        $data = [
            'shops' => $shops,
            'brandList' => $brandList,
            'years' => $years,
            'seasons' => $seasons,
            'allCategories' => $categories
        ];
        return $this->success($data);
    }
}